package com.chzc.fm.duotin.ui.home;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/28.
 */
public class ClickableTextView extends View {

    private Paint paint = new Paint();

    public ItemOnClickListener getItemOnClickListener() {
        return itemOnClickListener;
    }

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    //从来存储控件中文字所控制的区域
    private ItemOnClickListener itemOnClickListener;
    private List<TextDB> textLists = new ArrayList<>();

    public ClickableTextView(Context context) {
        super(context);
    }

    public ClickableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
    //    super.onDraw(canvas);
        //先画一个圆角矩形
        paint.setColor(Color.WHITE);
        //线宽
        paint.setStrokeWidth(4);
        //空心
        paint.setStyle(Paint.Style.STROKE);
        //抗锯齿
        paint.setAntiAlias(true);
        RectF rect = new RectF(0,0,getWidth(),getHeight());
       // rect.contains();
        canvas.drawRoundRect(rect,10,10,paint);
        //画线


        int width = getWidth();//控件宽度
        float linePostion = (float) (width/2.0);//中线的宽度
        //与控件的间隔
        int size = 10;
        int height = getHeight();
        canvas.drawLine(linePostion,10,linePostion,height-size*2,paint);
        //画文字
        //先计算坐标
        //左边的文字
        paint.setTextSize(30);
        paint.setStrokeWidth(2);
        int textLeftWidth = (int) (linePostion/3);
        int textRightWidth = (int) (linePostion+textLeftWidth);
        int heightText = getHeight()/2;
        canvas.drawText("订阅",textLeftWidth,heightText,paint);
        textLists.add(new TextDB(0,0,width/2,height));
       // Log.e("订阅"," ");
        canvas.drawText("分享",textRightWidth,heightText,paint);
        textLists.add(new TextDB(width/2,0,width,height));

    }




    private class TextDB {
        String text;
        int formX;
        int fromY;
        int endX;
        int endY;
        Rect rect;
        public TextDB(int formX,int fromY,int endX,int endY){
            this.formX = formX;
            this.fromY = fromY;
            this.endX = endX;
            this.endY = endY;
            rect = new Rect(formX,fromY,endX,endY);
        }

        public boolean include(int x,int y){

            Log.e("position","x = "+x+" y ="+y);

            return  (x>formX&&x<endX)&&(y>fromY&&y<endY);
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(setWidthSpec(widthMeasureSpec),setHeightSpec(heightMeasureSpec));
    }

    private int setHeightSpec(int heightMeasureSpec) {

        int specMode = MeasureSpec.getMode(heightMeasureSpec);
        int specSize = MeasureSpec.getSize(heightMeasureSpec);
        int result = 0;
        if(specMode==MeasureSpec.EXACTLY){

            result = specSize;
        }else{

            result = 100;
            if (specMode!=MeasureSpec.AT_MOST){
                result = Math.min(specSize,result);
            }

        }

        return result;
    }



    private int setWidthSpec(int widthMeasureSpec) {

        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        int result = 0;
        if(specMode==MeasureSpec.EXACTLY){

            result = specSize;
        }else{

            result = 220;
            if (specMode!=MeasureSpec.AT_MOST){
                result = Math.min(specSize,result);
            }

        }

        return result;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public boolean onTouchEvent(MotionEvent event) {


        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                int x = (int) event.getX();
                int y = (int) event.getY();
                Log.e("ClickPosition","x = "+x+" y = "+y);
                //判断
                if (itemOnClickListener!=null){
                 itemOnClickListener.onItemClick(getClickPostion(x,y));
                }

                return true;

        }


        return super.onTouchEvent(event);
    }


    public interface ItemOnClickListener{
        public void onItemClick(int index);
    }

    private int getClickPostion(int x, int y) {

        for (int i = 0; i < textLists.size(); i++) {
            if (textLists.get(i).include(x,y)){
                return i;
            }
        }
        return -1;
    }


}
